﻿using System;
using System.Collections.Generic;
using Authority.Logic;
using Authority.Model;
using Common.Library;
using Container.Library;
using Microsoft.AspNetCore.Mvc;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Authority.Controllers
{
    /// <summary>
    /// 部门功能管理
    /// </summary>
    [Area("Authority")]
    public class DepartmentController : BaseController
    {
        private readonly ISysDepartmentService departmentService = null;//部门功能逻辑
        public DepartmentController()
        {
            //部门功能逻辑注入
            departmentService = UnityCIContainer.Instance.GetService<ISysDepartmentService>();
        }


        #region 页面

        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 增加界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 修改界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Modify()
        {
            return View();
        }
        #endregion

        #region 查询操作

        /// <summary>
        /// 获取所有的可操作角色 
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult LoadListInfo()
        {
            var resultInfo = new ResultJsonInfo<List<DepartmentsListResponse>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                resultInfo = departmentService.LoadListInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        /// <summary>
        /// 获取部门树状列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadDepartmentTreeList()
        {
            var resultInfo = new ResultJsonInfo<List<TreeInfo>>();
            try
            {
                //实体接收数据
                resultInfo = departmentService.LoadTreeListInfo();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取所有的部门列表信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadDepartmentSelectList()
        {

            var resultInfo = new ResultJsonInfo<List<SelectListInfo>>();

            try
            {
                resultInfo = departmentService.LoadSelectListInfo();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 新增子节点
        /// <summary>
        /// 新增根节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.AddRoot)]
        public ActionResult AddRoot()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<DepartmentRootRequest>>();

                resultInfo = departmentService.AddRoot(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 新增子节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Add)]
        public ActionResult AddChild()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<DepartmentChildRequest>>();

                resultInfo = departmentService.AddChild(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 修改节点

        /// <summary>
        /// 修改节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Edit)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<DepartmentModifyRequest>>();

                resultInfo = departmentService.Modify(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 移动顺序操作
        /// 上移（1：上移/2：下移）
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Top)]
        public ActionResult UpMove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据.
                var parametersInfo = DeserializeRequestData<ParametersInfo<DepartmentMoveRequest>>();

                resultInfo = departmentService.Move(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 移动顺序操作
        /// 下移（1：上移/2：下移）
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Bottom)]
        public ActionResult DownMove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据.
                var parametersInfo = DeserializeRequestData<ParametersInfo<DepartmentMoveRequest>>();

                resultInfo = departmentService.Move(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 删除信息

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Remove)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<string>>>();

                resultInfo = departmentService.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

    }
}